﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CreateTable.cs" company="SmartPlan">
//   SmartPlan 2011
// </copyright>
// <summary>
//   The create table.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DbDiff.Common.Scripting
{
    using System;

    /// <summary>
    /// The create table.
    /// </summary>
    internal class CreateTable : SqlString
    {
        /// <summary>
        /// The before create.
        /// </summary>
        public readonly SqlString beforeCreate = new SqlString(string.Empty) ; // before creation

        /// <summary>
        /// The create begin.
        /// </summary>
        public readonly SqlString createBegin = new SqlString(string.Empty); // table header

        /// <summary>
        /// The field.
        /// </summary>
        public readonly SqlString field = new SqlString(string.Empty); // table field

        /// <summary>
        /// The after field.
        /// </summary>
        public readonly SqlString afterField = new SqlString(string.Empty); // after table fields

        /// <summary>
        /// The create end.
        /// </summary>
        public readonly SqlString createEnd = new SqlString(string.Empty); // table creation end

        /// <summary>
        /// The after create for bind.
        /// </summary>
        public readonly SqlString afterCreateForBind = new SqlString(string.Empty); // for binddefault and bindrule etc

        /// <summary>
        /// The after create for alter.
        /// </summary>
        public readonly SqlString afterCreateForAlter = new SqlString(string.Empty) ;// for alter table add etc

        /// <summary>
        /// Initializes a new instance of the <see cref="CreateTable"/> class.
        /// </summary>
        public CreateTable() : base(string.Empty)
        {
        }

        /// <summary>
        /// The add.
        /// </summary>
        /// <param name="crTable">
        /// The cr table.
        /// </param>
        public void Add(CreateTable crTable)
        {
            this.beforeCreate.AddSqlString(crTable.beforeCreate);
            this.createBegin.AddSqlString(crTable.createBegin);
            this.field.AddSqlString(crTable.field);
            this.afterField.AddSqlString(crTable.afterField);
            this.createEnd.AddSqlString(crTable.createEnd);
            this.afterCreateForBind.AddSqlString(crTable.afterCreateForBind);
            this.afterCreateForAlter.AddSqlString(crTable.afterCreateForAlter);
        }

        /// <summary>
        /// The to string.
        /// </summary>
        /// <returns>
        /// The to string.
        /// </returns>
        public override string ToString()
        {
            string createField = this.field.ToString();
            string afterInnerField = this.afterField.ToString();

            switch (afterInnerField.Length)
            {
                case 0:
                    if (createField.LastIndexOf(Comma) != -1)
                    {
                        createField = createField.Remove(createField.LastIndexOf(Comma), 1);
                    }
                    break;
                default:
                    afterInnerField = afterInnerField.Remove(afterInnerField.LastIndexOf(Comma), 1);
                    break;
            }
            
            return this.beforeCreate.ToString() +
                   this.createBegin + 
                   createField + 
                   afterInnerField +
                   this.createEnd + 
                   this.afterCreateForAlter +
                   this.afterCreateForBind;
        }
    }
}